package com.airconditioner.sales.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.airconditioner.sales.R;
import com.airconditioner.sales.model.ModelInfo;
import com.airconditioner.sales.util.GlideCircleTransform;
import com.airconditioner.sales.util.Url;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：shuizi_wade on 2017/1/19 13:54
 * 邮箱：674618016@qq.com
 */
public class SubClassAdapter extends RecyclerView.Adapter<SubClassAdapter.ItemViewHolder> {
    private Context context;
    private MyItemClickListener itemClickListener;
    private List<ModelInfo> mList = new ArrayList<>();

    public List<ModelInfo> getmList() {
        return mList;
    }

    public void setmList(List<ModelInfo> mList) {
        this.mList = mList;
    }

    public SubClassAdapter(Context context) {
        this.context = context;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_sub_class_list_row, null);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(v, (Integer) v.getTag());
            }
        });
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        ItemViewHolder viewHolder = holder;
        viewHolder.itemView.setTag(position);
        Glide.with(context).load(Url.baseUrl + mList.get(position).getMacIcon())
                .transform(new GlideCircleTransform(context)).into(viewHolder.imageView);
        viewHolder.tv_money.setText("￥" + mList.get(position).getMacAmount());
        viewHolder.tv_content.setText(mList.get(position).getMacIntro());
        viewHolder.tv_name.setText(mList.get(position).getMacName());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_name;
        public TextView tv_content;
        public TextView tv_money;
        public ImageView imageView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.sub_class_tv_title);
            tv_content = (TextView) itemView.findViewById(R.id.sub_class_tv_content);
            tv_money = (TextView) itemView.findViewById(R.id.sub_class_tv_money);
            imageView = (ImageView) itemView.findViewById(R.id.sub_class_iv);
        }
    }

    public interface MyItemClickListener {
        void onItemClick(View view, int postion);
    }

    public void setOnItemClickListener(MyItemClickListener listener) {
        this.itemClickListener = listener;
    }
}
